﻿@page "/scopes/{id}/{action}"
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.RealmStore;
@using SimpleIdServer.IdServer.Website.Stores.ScopeStore;
@using SimpleIdServer.IdServer.Website.Stores.UserStore;
@inherits Fluxor.Blazor.Web.Components.FluxorLayout
@inject NotificationService notificationService
@inject NavigationManager navigationManager
@layout MainLayout
@inject IState<ScopeState> scopeState
@inject IDispatcher dispatcher

<RadzenBreadCrumb>
    <RadzenBreadCrumbItem Path="/scopes" Text="@Global.Scopes" />
    <RadzenBreadCrumbItem Text="@Global.ScopeDetails" />
</RadzenBreadCrumb>

<RadzenText class="mt-3 mb-3 no-margin" Text="@scopeState.Value.Scope?.Name" TextStyle="TextStyle.DisplayH3" />

<div class="mb-1">
    @if (scopeState.Value.Scope.Type == ScopeTypes.IDENTITY)
    {
        <RadzenBadge Text="@Global.IdentityScope" IsPill="true" />
    }
    else
    {
        <RadzenBadge Text="@Global.ApiScope" IsPill="true" />
    }

    @if (scopeState.Value.Scope.Protocol == ScopeProtocols.OPENID)
    {
        <RadzenBadge Text="@Global.OpenId" IsPill="true" />
    }
    else if (scopeState.Value.Scope.Protocol == ScopeProtocols.SAML)
    {
        <RadzenBadge Text="@Global.Saml" IsPill="true" />
    }
    else if (scopeState.Value.Scope.Protocol == ScopeProtocols.OAUTH)
    {
        <RadzenBadge Text="@Global.OAuth" IsPill="true" />
    }
</div>

<RadzenCardLoader Variant="Variant.Filled" IsLoading=scopeState.Value.IsLoading>
    <RadzenTabs Change=@OnChange @bind-SelectedIndex="selectedIndex">
        <Tabs>
            <RadzenTabsItem Text="@Global.Details">
                <ScopeSettings Scope="@scopeState.Value.Scope"></ScopeSettings>
            </RadzenTabsItem>
            @if (scopeState.Value.Scope.Type == ScopeTypes.IDENTITY)
            {
                <RadzenTabsItem Text="@Global.Mappers">
                    <ScopeMappers Scope="@scopeState.Value.Scope"></ScopeMappers>
                </RadzenTabsItem>
            }
            else if(scopeState.Value.Scope.Type == ScopeTypes.APIRESOURCE)
            {
                <RadzenTabsItem Text="@Global.ApiResources">
                    <ScopeResources Scope="@scopeState.Value.Scope"></ScopeResources>
                </RadzenTabsItem>
            }
        </Tabs>
    </RadzenTabs>
</RadzenCardLoader>

@code {
    Dictionary<string, (int, IEnumerable<ScopeTypes>)> mappingActionNameToIndex = new Dictionary<string, (int, IEnumerable<ScopeTypes>)>
    {
        { "settings", (0, new List<ScopeTypes> { ScopeTypes.IDENTITY, ScopeTypes.APIRESOURCE })},
        { "mappers", (1, new List<ScopeTypes> { ScopeTypes.IDENTITY })},
        { "resources", (1, new List<ScopeTypes> { ScopeTypes.APIRESOURCE })}
    };

    [Parameter]
    public string id { get; set; } = null!;

    [Parameter]
    public string? action { get; set; } = null;

    int selectedIndex = 0;

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if(firstRender)
        {
            SubscribeToAction<GetScopeFailureAction>((act) =>
            {
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = act.ErrorMessage });
                StateHasChanged();
            });
            SubscribeToAction<SelectRealmSuccessAction>((act) =>
            {
                navigationManager.NavigateTo("/scopes");
            });
        }
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (string.IsNullOrWhiteSpace(id)) return;
        if (!string.IsNullOrWhiteSpace(action) && mappingActionNameToIndex.ContainsKey(action))
            selectedIndex = mappingActionNameToIndex[action].Item1;
        dispatcher.Dispatch(new GetScopeAction { ScopeId = id });
    }

    void OnChange(int index)
    {
        var rec = mappingActionNameToIndex.Single(kvp => kvp.Value.Item1 == index && kvp.Value.Item2.Contains(scopeState.Value.Scope.Type));
        navigationManager.NavigateTo($"/scopes/{id}/{rec.Key}");
    }
}